Skip to content

最初のPOSTリクエスト

このチュートリアルでは、0rbitプロセスでPOSTリクエストを行う方法を学びます。

🔑 前提条件

  • システムにaosがインストールされていること。
  • 一部の$0RBT。こちらで$0RBTを入手する方法を学ぶこちら_
  • 任意のコードエディタ(VSCode、Sublime Textなど)

上記の前提条件が整ったら、

🛠️ 開発を始めましょう

プロジェクトの初期化

プロジェクトディレクトリに0rbit-Post-Request.luaという新しいファイルを作成します。

bash
touch 0rbit-Post-Request.lua

変数の初期化

lua
local json = require("json")

_0RBIT = "BaMK1dfayo75s3q1ow6AO64UDpD9SEFbeE8xYrY2fyQ"
_0RBT_POINTS = "BUhZLMwQ6yZHguLtJYA5lLUa9LQzLXMXRfaq9FVcPJc"

FEE_AMOUNT = "1000000000000" -- 1 $0RBT
BASE_URL = "https://arweave.dev/graphql"
-- The data body to be sent in the POST request
BODY = json.encode({
    query = [[
        query {
            transactions(
                owners: ["vh-NTHVvlKZqRxc8LyyTNok65yQ55a_PJ1zWLb9G2JI"]
            ) {
                edges {
                    node {
                        id
                    }
                }
            }
        }
    ]]
});

ReceivedData = ReceivedData or {}

リクエストの送信

以下のコードには、0rbitプロセスに1 $0RBTを送信し、BASE_URLに対してPOSTリクエストを行うハンドラが含まれています。

lua
Handlers.add(
    "Post-Request",
    Handlers.utils.hasMatchingTag("Action", "First-Post-Request"),
    function(msg)
        Send({
            Target = _0RBT_TOKEN,
            Action = "Transfer",
            Recipient = _0RBIT,
            Quantity = FEE_AMOUNT,
            ["X-Url"] = BASE_URL,
            ["X-Action"] = "Post-Real-Data",
            ["X-Body"] = BODY
        })
        print(Colors.green .. "You have sent a POST Request to the 0rbit process.")
    end
)

上記のコードの内訳:

  • Handlers.addは、aoプロセスに新しいハンドラを追加するために使用されます。

  • Post-Request__はハンドラの名前です。

  • Handlers.utils.hasMatchingTagは、受信メッセージがFirst-Post-Requestと同じタグを持っているかをチェックする関数です。

  • function(msg)は、ハンドラが呼び出されたときに実行される関数です。

  • Sendは、いくつかのタグを引数として取り、ao上にメッセージを作成する関数です。

    TagDescription
    TargetThe processId of the recipient. In this case, it's the $0RBT token processId.
    ActionThe tag that defines the handler to be called in the recipient process. In this case it's Transfer
    RecipientThe tag that accepts the processId to whom the $0RBT will be sent. In this case, it's the 0rbit processId.
    QuantityThe amount of $0RBT to be sent.
    ["X-Url"]The forwarded-tag which contains the URL and the same will be used by the 0rbit process to fetch the data.
    ["X-Action"]The forwarded-tag which contains the action to be performed by the 0rbit process. In this case, it's Post-Real-Data.
    ["X-Body"]The forwarded-tag which contains the data body to be sent in the POST request.

データの受信

以下のコードには、0rbitプロセスからデータを受信して印刷するハンドラが含まれています。

lua
Handlers.add(
    "Receive-Data",
    Handlers.utils.hasMatchingTag("Action", "Receive-Response"),
    function(msg)
        local res = json.decode(msg.Data)
        ReceivedData = res
        print(Colors.green .. "You have received the data from the 0rbit process.")
    end
)

上記のコードの内訳:

  • Handlers.addは、aoプロセスに新しいハンドラを追加するために使用されます。
  • Receive-Dataはハンドラの名前です。
  • Handlers.utils.hasMatchingTagは、受信メッセージがReceive-Responseと同じタグを持っているかをチェックする関数です。
  • function(msg)は、ハンドラが呼び出されたときに実行される関数です。
    • json.decodeは、受信したJSONデータをデコードするために使用されます。
    • ReceivedData = resは、受信したデータをReceivedData変数に格納します。

0rbitプロセスは常にデータをstring形式で送信します。
上記では、受信データが文字列化されたJSONであるためjson.decodeが使用されています。
そのため、要件に応じてデータをデコードする必要があります。

🏃 プロセスを実行する

新しいプロセスを作成し、スクリプトを読み込む

bash
aos 0rbitPostRequest --load 0rbit-Post-Request.lua

上記のコマンドは、0rbitPostRequestという名前の新しいプロセスを作成し、0rbit-Post-Request.luaを読み込みます。

プロセスに資金を提供する

いくつかの$0RBTをあなたのプロセスIDに転送します。

ハンドラの呼び出し

ハンドラを呼び出して、0rbitプロセスへのリクエストを作成します。

bash
Send({ Target= ao.id, Action="First-Post-Request" })

データの確認

ReceivedData変数に格納されたデータを確認するには、以下のコマンドを実行します:

bash
ReceivedData

成功裏に実行されると、ターミナルにJSONデータが表示されます:

json
{
    data = {
        transactions = {
        edges = {
            {
            node = {
                id = "nH0NU9rgNqGVHwjtjFvnIyXpsP7YVrj_v7JxFErHNB4"
            }
            },
            //and so on...
            {
            node = {
                id = "9HLUVJo4AcrSxQeapf2hutS2Xp7hx_XDiIvv3vnxDcc"
            }
            }
        }
        }
    }
}

やった!あなたは成功裏に0rbitプロセスで最初のPOSTリクエストを行いました。🎉

You can find the complete code here:

https://github.com/0rbit-co/examples/blob/main/First-Post-Request.lua